#!/bin/bash

# check-mta-track.sh
# 
# Script to check that mta-track has tracked all servers. If it
# hasn't, the lines from log-server.txt that need to be requeried are
# produced at the stdout. Otherwise, the output is empty.
#
# Syntax
#
#    ./check-mta-track.sh log-server.txt log-mta.txt
#
# v0.1  7 Sep 2007
#
# Author: Ramon Casero <ramon.casero@oucs.ox.ac.uk>
# Copyright © 2007 University of Oxford
#
# This file is part of mta-tracker.
#
# mta-tracker is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# mta-tracker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with mta-tracker.  If not, see <http://www.gnu.org/licenses/>.
#
# ChangeLog
#
# * 2008-08-21 (rcasero):
#
#   ** Upgrade licence to GPL v3.
#
# * 2007-09-07 (rcasero): v0.1.

# syntax message
usage() {
    echo "Syntax: `basename $0` log-server.txt log-mta.txt"
}

# check number of input arguments
if [ ! $# -eq 2 ]
    then
    usage
    exit 1
fi

# temp files for intermediate results
STEMP=`mktemp -t -q check-mta-track.XXXXXXXX`
if [ $? -ne 0 ]; then
    echo Error: unable to create temporary file
    exit 1
fi
MTEMP=`mktemp -t -q check-mta-track.XXXXXXXX`
if [ $? -ne 0 ]; then
    echo Error: unable to create temporary file
    exit 1
fi
DTEMP=`mktemp -t -q check-mta-track.XXXXXXXX`
if [ $? -ne 0 ]; then
    echo Error: unable to create temporary file
    exit 1
fi

# alias input arguments
slog="$1"
mlog="$2"

# get list of servers that have to be queried
cut -f 3 "$slog" \
    | grep -v -e 'no MX record' -e 'host not found for domain' \
    | sort | uniq > "$STEMP"

# get list of servers that have been queried
cut -f 1 "$mlog" | sort | uniq > "$MTEMP"

# get list of servers that have not been queried
grep -v -f "$MTEMP" "$STEMP" > "$DTEMP"

# get lines of log-server.txt that need to be queried
grep -f "$DTEMP" "$slog"

# delete temp files
rm -f "$DTEMP" "$MTEMP" "$STEMP"
